package com.kingdee.eas.web.atsexceptionbillList.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.atsexc.AtsExceptionBillFactory;
import com.kingdee.eas.custom.atsexc.AtsExceptionBillInfo;
import com.kingdee.eas.custom.atsexc.IAtsExceptionBill;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;

public  class TestSubmitAction extends EditHandler {
	public void submitEffectAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		AtsExceptionBillInfo model = (AtsExceptionBillInfo) request
				.getAttribute("dynamic_model");
		String uuid = null;
		if (model.getId() != null) {
			uuid = model.getString("id");
		}

		if (StringUtils.isEmpty(uuid)) {
			// 直接点击的提交生效 需要先进行保存
			super.saveAction(request, response, modelMap);
			uuid = model.getId().toString();

		}
		updateBillState(new ObjectUuidPK(uuid), 3);
	}

	private void updateBillState(IObjectPK pk, Integer state)
			throws SHRWebException {
		IAtsExceptionBill instance;
		try {
			instance = AtsExceptionBillFactory.getRemoteInstance();
			AtsExceptionBillInfo applicationBillInfo = instance
					.getAtsExceptionBillInfo(pk);
			switch (state) {

			case 0: // 未提交 saved

				applicationBillInfo.setBillState(HRBillStateEnum.SAVED);

				break;

			case 1: // 未审批 submited

				applicationBillInfo.setBillState(HRBillStateEnum.SUBMITED);

				break;
			case 2: // 审批中 auditing

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITING);

				break;
			case 3: // 审批通过 audited

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITED);

				break;
			case 4: // 审批未通过 auditend

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITEND);

				break;

			default:

				applicationBillInfo.setBillState(HRBillStateEnum.SAVED);

				break;

			}
			instance.save(applicationBillInfo);

		} catch (EASBizException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		}
	}

}
